package org.jeecg.modules.lab.items.record.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spire.office.packages.sprllg;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.lab.device.entity.LabDevice;
import org.jeecg.modules.lab.device.mapper.LabDeviceMapper;
import org.jeecg.modules.lab.entrust.entity.LabEntrustItems;
import org.jeecg.modules.lab.entrust.entity.LabItemsRecordDevice;
import org.jeecg.modules.lab.entrust.entity.LabItemsRecordPerson;
import org.jeecg.modules.lab.items.entity.LabItemsDevice;
import org.jeecg.modules.lab.items.item.mapper.LabItemsDeviceMapper;
import org.jeecg.modules.lab.items.record.mapper.LabItemsRecordDeviceMapper;
import org.jeecg.modules.lab.items.record.service.ILabItemsRecordDeviceService;
import org.jeecg.modules.lab.items.temp.entity.LabItemsTempPerson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description: 项目检测记录_设备器具信息
 * @Author: jeecg-boot
 * @Date: 2023-10-08
 * @Version: V1.0
 */
@Service
public class LabItemsRecordDeviceServiceImpl extends ServiceImpl<LabItemsRecordDeviceMapper, LabItemsRecordDevice> implements ILabItemsRecordDeviceService {

    @Autowired
    private LabItemsRecordDeviceMapper labItemsRecordDeviceMapper;

    @Autowired
    private LabItemsDeviceMapper labItemsDeviceMapper;

    @Autowired
    private LabDeviceMapper labDeviceMapper;


    @Override
    public List<LabItemsRecordDevice> handleDevData(String entrustId, String taskItemId, String itemId) {
        QueryWrapper<LabItemsRecordDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.clear();
        if (!"".equals(taskItemId) || taskItemId != null) {
            queryWrapper.eq("task_item_id", taskItemId);
        }
        queryWrapper.eq("task_id", entrustId)
                .eq("item_id", itemId)
                .orderByAsc("create_time");
        List<LabItemsRecordDevice> labItemsRecordDeviceList = labItemsRecordDeviceMapper.selectList(queryWrapper);
        return labItemsRecordDeviceList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(String entrustId, String taskitemId, String itemId, List<LabItemsRecordDevice> recordDevList) {
        //1、先根据委托/监督Id、项目id条件删除数据
        QueryWrapper<LabItemsRecordDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.clear();
        queryWrapper.eq("task_id", entrustId)
                .eq("task_item_id", taskitemId)
                .eq("item_id", itemId);
        labItemsRecordDeviceMapper.delete(queryWrapper);
        //2、重新插入数据
        if (recordDevList != null && recordDevList.size() > 0) {
            for (LabItemsRecordDevice recordDev : recordDevList) {
                recordDev.setTaskId(entrustId);
                recordDev.setTaskItemId(taskitemId);
                recordDev.setItemId(itemId);
                labItemsRecordDeviceMapper.insert(recordDev);
            }
        }
    }

    /**
     * 根据委托id和项目id查询数据
     *
     * @param id
     * @return
     */
    @Override
    public List<LabItemsRecordDevice> byEntrustItemId(String id) {
        QueryWrapper<LabItemsRecordDevice> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("task_item_id", id);
        return labItemsRecordDeviceMapper.selectList(objectQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertDeviceData(LabEntrustItems labEntrustItems) {
        String task_id = labEntrustItems.getEntrustId();
        String task_item_id = labEntrustItems.getId();
        String item_id = labEntrustItems.getItemId();
        String item_name = labEntrustItems.getItemName();

        QueryWrapper<LabItemsDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id", item_id);
        List<LabItemsDevice> itemsDeviceList = labItemsDeviceMapper.selectList(queryWrapper);
        List<String> devIds = new ArrayList<>();
        if (itemsDeviceList.size() > 0) {
            for (LabItemsDevice itemsDevice : itemsDeviceList) {
                devIds.add(itemsDevice.getDevId());
            }
        }
        if (devIds.size() > 0) {
            QueryWrapper<LabDevice> queryWrapperA = new QueryWrapper<>();
            queryWrapperA.in("id", devIds);
            List<LabDevice> deviceList = labDeviceMapper.selectList(queryWrapperA);

            for (LabDevice device : deviceList) {
                // TODO: 处理每个实例 LabItemsDevice
                LabItemsRecordDevice labItemsRecordDevice = new LabItemsRecordDevice();
                labItemsRecordDevice.setTaskId(task_id);
                labItemsRecordDevice.setTaskItemId(task_item_id);
                labItemsRecordDevice.setItemId(item_id);
                labItemsRecordDevice.setItemName(item_name);
                labItemsRecordDevice.setDevId(device.getId());
                labItemsRecordDevice.setDevName(device.getDevName());
                labItemsRecordDevice.setDevNum(device.getDevNumber());
                if (device.getEndCheckDate() != null && !"".equals(device.getEndCheckDate())) {
                    labItemsRecordDevice.setDevValidity(DateUtils.formatDate(device.getEndCheckDate()));
                }
                labItemsRecordDevice.setSynFlag("0");
                labItemsRecordDeviceMapper.insert(labItemsRecordDevice);
            }
        }
    }

    @Override
    public void deleteByTaskId(String taskId) {
        labItemsRecordDeviceMapper.deleteByTaskId(taskId);
    }
}
